package com.library_manage_system.strategy;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.GetBookByName;
import com.library_manager_system.utils.LibraryDao;

public class BorrowBookPanelStrategy implements PanelStrategy {

	@Override
	public JPanel createPanel(final ArrayList<Book> arraylist,final WindowsMainImpl windowsMainImpl) {
		// TODO Auto-generated method stub
		
		JPanel borrowBookPanel = new JPanel();
		borrowBookPanel.setLayout(new FlowLayout());
		// 设置一个label来提示客户信息，提问想要借哪一本书
		JLabel jLabel = new JLabel("请问你想要借哪一本书");

		// 设置一个下拉框用来选择客户想要借哪一本书
		int size = arraylist.size();
		String[] s = new String[size];
		Iterator<Book> it = arraylist.iterator();
		while (it.hasNext()) {
			s[size - 1] = it.next().getBook_name();
			size--;
		}
		// {"中国人物研究","java从入门到放弃","程序员日常撸代码"};
		final JComboBox jComboBox = new JComboBox(s);
		
		JScrollPane jScrollPane = new JScrollPane(jComboBox);

		// 设置一个按钮表示提交想要借的书
		JButton jButton = new JButton("确定");
		jButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String borrow_book = (String) jComboBox.getSelectedItem();
				Book bo_book = GetBookByName.getBookByName(arraylist,
						borrow_book);
				// 借出的书数量减1
				if(bo_book == null){
					JOptionPane.showMessageDialog(null, "这本书已经全部借出去了，请点其他界面刷新一下！", 
							"图书馆管理系统",JOptionPane.ERROR_MESSAGE);
					return;
				}
				int the_rest_number = bo_book.borrow();
				if (the_rest_number == 0) {
					// 如果这本书剩下的数量为0,那么就先把他保存进Statics_borrow_book.txt中
					// LibraryDao.writeBook(bo_book, 2);
					// 把借出的书从arraylist中删除
					arraylist.remove(bo_book);
					LibraryDao.updateLibraryDao(arraylist, 1);

				}
				ArrayList<Book> list = LibraryDao.readBook(2);
				Iterator<Book> it1 = list.iterator();
				int temp = 0;
				while (it1.hasNext()) {
					Book book = it1.next();
					if (book.getBook_name().equals(bo_book.getBook_name())) {
						book.borrow();
						LibraryDao.updateLibraryDao(list, 2);
						temp = 1;
						break;
					}
				}
				if (temp == 0) {
					System.out.println(bo_book.getBorrow_book_number());
					list.add(bo_book);
					System.out.println("我进来了");
					LibraryDao.updateLibraryDao(list, 2);
				}
				LibraryDao.updateLibraryDao(arraylist, 1);
				//WindowsMainImpl.init(arraylist windowsM);
				windowsMainImpl.bookListChange(arraylist);
				
				JOptionPane.showMessageDialog(null, "借出成功");
			}
		});
		// jButton.setPreferredSize(new Dimension(50,30));
		// jButton.setLocation(50, 50);

		JPanel jPanel = new JPanel();
		jPanel.setLayout(new GridLayout(3, 1, 50, 50));
		jPanel.add(jLabel);
		jPanel.add(jScrollPane);
		jPanel.add(jButton);

		borrowBookPanel.add(jPanel);

		return borrowBookPanel;
	}
}
